package com.ddwl.shop.dao;

import com.alibaba.fastjson.JSON;
import com.ddwl.common.constant.ShopEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.exception.GlobalException;
import com.ddwl.schema.bo.shop.QueryShopApproveBo;
import com.ddwl.shop.dao.mapper.ShopApproveMapper;
import com.ddwl.shop.dao.model.ShopApprove;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.time.Instant;
import java.util.Arrays;
import java.util.List;

/**
 * (ShopApprove)表数据库访问层
 *
 * @author wenbin
 */
@Slf4j
@Repository
public class ShopApproveDao extends TkDao<ShopApproveMapper, ShopApprove> {

    public void updateShopApprove(ShopApprove shopApprove) {
        Example example = new Example(ShopApprove.class);
        example.and().andEqualTo("id", shopApprove.getId())
                .andEqualTo("userId", shopApprove.getUserId());
        Integer i = baseMapper.updateByExampleSelective(shopApprove, example);
        if (i < 1) {
            log.error("修改申请失败 approveId={}", JSON.toJSONString(shopApprove.getId()));
            throw new GlobalException("修改申请失败");
        }
    }

    public void cancelShopApprove(String approveId, String userId) {
        ShopApprove shopApprove = new ShopApprove();
        shopApprove.setApproveStatus(ShopEnum.ApproveStatus.CANCEL.getCode());
        shopApprove.setGmtModified(Instant.now());
        Example example = new Example(ShopApprove.class);
        example.and().andEqualTo("id", approveId);
        //.andEqualTo("userId",userId);
        Integer i = baseMapper.updateByExampleSelective(shopApprove, example);
        if (i < 1) {
            log.error("取消申请失败 approveId={}", JSON.toJSONString(approveId));
            throw new GlobalException("取消申请失败");
        }
    }

    public List<ShopApprove> queryShopApproveList(QueryShopApproveBo bo) {
        List<ShopApprove> shopApproves = baseMapper.queryShopApproveList(bo);
        return shopApproves;
    }

    public ShopApprove queryOrderShopApprove(QueryShopApproveBo bo) {
        ShopApprove shopApproves = baseMapper.queryOrderShopApprove(bo);
        return shopApproves;
    }

    public ShopApprove queryShopApprove(String userId, Integer approveStatus) {
        Example example = getExample();
        example.and()
                .andEqualTo("userId", userId)
                .andNotEqualTo("approveStatus", approveStatus);
        return baseMapper.selectOneByExample(example);
    }

    public ShopApprove queryShopApprove(String id, String userId) {
        Example example = getExample();
        example.and()
                .andEqualTo("userId", userId)
                .andEqualTo("id", id);
        return baseMapper.selectOneByExample(example);
    }

    public void submitShopApprove(String approveId) {
        ShopApprove shopApprove = new ShopApprove();
        shopApprove.setId(approveId);
        shopApprove.setApproveStatus(ShopEnum.ApproveStatus.APPLY.getCode());
        shopApprove.setGmtModified(Instant.now());
        Integer i = baseMapper.updateByPrimaryKeySelective(shopApprove);
        if (i < 1) {
            log.error("提交申请失败 approveId={}", JSON.toJSONString(approveId));
            throw new GlobalException("提交申请失败");
        }
    }

    public ShopApprove queryByName(String name) {
        Example example = getExample();
        example.and()
                .andEqualTo("name", name)
                .andIn("approveStatus", Arrays.asList(ShopEnum.ApproveStatus.APPLY.getCode(), ShopEnum.ApproveStatus.PASS.getCode()));
        return baseMapper.selectOneByExample(example);
    }
}